/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$       : main.c                                         	      *
 *  $Author$          : ChipON AE/FAE Group                                   *
 *  $Data$            : 2021-07-8                                             *
 *  $Hard Version     : KF32A156-MINI-EVB_V1.2                                *
 *  $Description$     : Flash the LED0 every 500ms on the KF32A156 Mini EVB,  *
 * 						and press the key to flip another LED1				  *
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyright protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *                     		REVISON HISTORY                               	  *
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  Data       Version  Author        Description                             *
 *  ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  2021-07-08 00.02.00 FAE Group     Version 2.0 update                      *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/

/******************************************************************************
**                      		Include Files                                **
******************************************************************************/
#include "stdio.h"
#include "system_init.h"
#include "Stop0.h"

/******************************************************************************
**                      	Private variables                                **
******************************************************************************/

#define LED1_PORT GPIOD_SFR /* PD12-PIN */
#define LED1_PIN_MASK GPIO_PIN_MASK_12

#define LED2_PORT GPIOH_SFR
#define LED2_PIN_MASK GPIO_PIN_MASK_3

#define LED1_ON() \
	;             \
	GPIO_Set_Output_Data_Bits(LED1_PORT, LED1_PIN_MASK, Bit_SET);
#define LED1_OFF() \
	;              \
	GPIO_Set_Output_Data_Bits(LED1_PORT, LED1_PIN_MASK, Bit_RESET);
#define LED1_Toggle() \
	;                 \
	GPIO_Toggle_Output_Data_Config(LED1_PORT, LED1_PIN_MASK);

#define LED2_ON() \
	;             \
	GPIO_Set_Output_Data_Bits(LED2_PORT, LED2_PIN_MASK, Bit_SET);
#define LED2_OFF() \
	;              \
	GPIO_Set_Output_Data_Bits(LED2_PORT, LED2_PIN_MASK, Bit_RESET);
#define LED2_Toggle() \
	;                 \
	GPIO_Toggle_Output_Data_Config(LED2_PORT, LED2_PIN_MASK);

/*******************************************************************************
**                     		   Global Functions 		             	      **
*******************************************************************************/
/**
 *  @brief :GPIO input initialize.
 *  @param in :	GPIOx : IO grop
				GpioPin:IO Pin
 *  @param out :None
 *  @retval :None
 */
void GPIOInit_Input_Config(GPIO_SFRmap *GPIOx, uint16_t GpioPin)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_Struct_Init(&GPIO_InitStructure);
	GPIO_InitStructure.m_Pin = GpioPin;
	GPIO_InitStructure.m_Speed = GPIO_LOW_SPEED;
	GPIO_InitStructure.m_Mode = GPIO_MODE_IN;
	GPIO_InitStructure.m_PullUp = GPIO_NOPULL;
	GPIO_InitStructure.m_PullDown = GPIO_NOPULL;
	GPIO_Configuration(GPIOx, &GPIO_InitStructure);
}

/**
 *  @brief :GPIO output initialize.
 *  @param in :	GPIOx : IO grop
				GpioPin:IO Pin
 *  @param out :None
 *  @retval :None
 */
void GPIOInit_Output_Config(GPIO_SFRmap *GPIOx, uint16_t GpioPin)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_Struct_Init(&GPIO_InitStructure);
	GPIO_InitStructure.m_Pin = GpioPin;
	GPIO_InitStructure.m_Speed = GPIO_LOW_SPEED;
	GPIO_InitStructure.m_Mode = GPIO_MODE_OUT;
	GPIO_InitStructure.m_PullUp = GPIO_NOPULL;
	GPIO_InitStructure.m_PullDown = GPIO_NOPULL;
	GPIO_Configuration(GPIOx, &GPIO_InitStructure);
	GPIO_Set_Output_Data_Bits(GPIOx, GpioPin, Bit_RESET);
}

/*******************************************************************************
**                     			Main Functions 		             	     	  **
*******************************************************************************/
/**
 *  @brief :Main program
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
int main()
{
	uint16_t temp_cnt = 0;

	/* Initialize the system clock is 120M*/
	SystemInit(120);

	/* Setup SysTick Timer as delay function, and input frequency is 120M */
	systick_delay_init(120);
	/* Initialize GPIO */
	GPIOInit_Input_Config(GPIOD_SFR, GPIO_PIN_MASK_2);
	GPIO_Pull_Up_Enable(GPIOD_SFR, GPIO_PIN_MASK_2, TRUE);

	GPIOInit_Output_Config(LED1_PORT, LED1_PIN_MASK);

	GPIOInit_Output_Config(LED2_PORT, LED2_PIN_MASK);

	while (1)
	{
		if (GPIO_Read_Input_Data_Bit(GPIOD_SFR, GPIO_PIN_MASK_2) == Bit_SET)
		{
			systick_delay_ms(10);
			if (GPIO_Read_Input_Data_Bit(GPIOD_SFR, GPIO_PIN_MASK_2) == Bit_SET)
			{
				LED1_OFF();
				LED2_OFF();

				Goto_Stop0_test();


				LED1_ON();
				for (int i = 0; i < 10; i++)
				{
					LED1_Toggle();
					LED2_Toggle();
					systick_delay_ms(200);
				}
			}
		}
		else if (GPIO_Read_Input_Data_Bit(GPIOD_SFR, GPIO_PIN_MASK_2) == Bit_RESET)
		{
			systick_delay_ms(10);
			if (GPIO_Read_Input_Data_Bit(GPIOD_SFR, GPIO_PIN_MASK_2) == Bit_RESET)
			{
				LED2_OFF();
				LED1_Toggle();
			}
		}
		systick_delay_ms(500);
	}
}

/**
 *  @brief : Reports the name of the source file and the source line number
 *           where the assert_param error has occurred.
 *  @param in :	file pointer to the source file name
 * 	@param in :	line assert_param error line source number
 *  @param out :None
 *  @retval :None
 */
void check_failed(uint8_t *File, uint32_t Line)
{
	/* User can add his own implementation to report the file name and line number,
		ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while (1)
	{
		;
	}
};


